<style>
    .channelDetailp, .menuPageBox {
        border:2px solid #000;
        width:fit-content;
        padding:8px 10px;
		color:#000;
    }
    .channelDetailp h1 {
		margin:0px;
		padding:0px;
	}
    .channelDetaila {
        text-decoration:none;
        color:#000;
    }
    .itemDetailp {
        padding:0px;
        margin:5px 0px;
    }
    .itemDetailp h3 {
		margin:0px;
		padding:0px;
	}
    .itemDetaila {
        text-decoration:none;
        color:#000;
    }
    img{
        max-width:200px;
    }
	.urlin {
		border:2px solid #000;
        width:fit-content;
        padding:8px 10px;
		color:#000;
	}
</style>

<?php 
	@$q = $_GET["url"];
	@$p = $_GET["page"];
	@$g = $_GET["img"];
	$page = 6;
	if (!$q){ ?>
		<form action="./" method="GET" class="urlin">
			<select name="url">
				<option value="https://www.ithome.com/rss/">IT之家</option>
			</select>
			&nbsp;&nbsp;
			<input type="checkbox" name="img" value="on" id="lb"/>
				<label for="lb">显示图片</label>
			&nbsp;&nbsp;
			<input type="submit" value="获取"/>
		</form>
		<br>
<?php } else {?>
		<input type="button" onclick="location.href='./?url=&page='" value="返回"/><br><br>
<?php }?>


<?php
	//find out which feed was selected
	if ($q) {
		$xml = $q;
	} else {
	    echo("<i>Empty</i>");
		die();
	}
	if (!$p || $p < 0 || !is_numeric($p)) { $p = 0; }
	
	$xmlDoc = new DOMDocument();
	$xmlDoc->load($xml);
	
	//get elements from "<channel>"
	$channel = $xmlDoc->getElementsByTagName('channel')->item(0);
	$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
	$channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
	$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
	
	//output elements from "<channel>"
	echo("<div class='channelDetailp'><a class='channelDetaila' href='" . $channel_link . "'><h1>" . $channel_title . "</h1></a>");
	echo($channel_desc . "</div><br/>");
	
	//get and output "<item>" elements
	$x = $xmlDoc->getElementsByTagName('item');
	
	$item_num = $x->length - 1;
	$from = $page * $p;
	$to = $page * ($p + 1);
	
	if ($from > $item_num) {
		echo("[ERROR] Out Of Page!");
	} else if ($from <= $item_num && $to > $item_num) {
		$to = $item_num;
	} else {
		echo("");
	}
	
	for ($i = $from; $i < $to; $i ++) {
		$item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
		$item_link=$x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
		$item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
		
		$item_desc = str_replace("?x-bce-process=image/format,f_auto","",$item_desc);
		$item_desc = str_replace("<img src=\"","<img src=\"./getimg.php?url=",$item_desc);
		
		if (!$g || $g == "off") { $item_desc = preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', '', $item_desc); }
		
		echo("<p class='itemDetailp'><a class='itemDetaila' href='" . $item_link . "'><h3>" . $item_title . "</h3></a>");
		echo($item_desc . "</p><hr>");
	}
?>

<table class="menuPageBox" width="60%"><tr>
	<td align="left">
		<a href="./?url=<?php echo($q); ?>&page=<?php echo($p - 1); ?>">上一页</a>
	</td>
	<td align="right">
		<a href="./?url=<?php echo($q); ?>&page=<?php echo($p + 1); ?>">下一页</a>
	</td>
<tr></table>